Return to start page

Systems/Gui/Struct Dialog.j

Code

		
1			library AStructSystemsGuiDialog requires optional ALibraryCoreDebugMisc, AStructCoreGeneralHashTable, AStructCoreGeneralVector, ALibraryCoreStringConversion
2
3 /**
4 * Provides a kind of wrapper struct for common Warcraft 3 dialogs.
5 * Actually it's not only a wrapper since it's expands the whole functionality of dialogs by using @struct ADialogButton instances.
6 * Besides it allows user to add more than 12 (or 16? Warcraft 3 limit) buttons by adding next and previous page buttons and splitting
7 * all 10-button groups into pages.
8 */
9 struct ADialog
10 //static constant members
11 public static constant integer maxDialogButtons = 100
12 private static constant integer maxPageButtons = 10
13 //static start members
14 private static integer shortcutPreviousPage
15 private static integer shortcutNextPage
16 private static string textPreviousPage
17 private static string textNextPage
18 //dynamic members
19 private string m_message
20 private boolean m_isDisplayed
21 //start members
22 private player m_player
23 //members
24 private dialog m_dialog
25 private AIntegerVector m_dialogButtons
26 private integer m_currentPage
27 private integer m_maxPageNumber
28 private button m_previousPageButton
29 private trigger m_nextPageTrigger
30 private button m_nextPageButton
31 private trigger m_previousPageTrigger
32
33 //! runtextmacro optional A_STRUCT_DEBUG("\"ADialog\"")
34
35 //dynamic members
36
37 public method setMessage takes string message returns nothing
38 set this.m_message = message
39 call DialogSetMessage(this.m_dialog, message) //InsertLineBreaks(message, 10)
40 endmethod
41
42 public method message takes nothing returns string
43 return this.m_message
44 endmethod
45
46 public method setDisplayed takes boolean displayed returns nothing
47 local integer i
48 local integer exitValue
49 set this.m_isDisplayed = displayed
50 if (displayed) then
51 set i = this.m_currentPage * thistype.maxPageButtons
52 set exitValue = (this.m_currentPage + 1) * thistype.maxPageButtons
53 loop
54 exitwhen (i == exitValue or i == this.m_dialogButtons.size())
55 call ADialogButton(this.m_dialogButtons[i]).addButton()
56 set i = i + 1
57 endloop
58 if (this.m_maxPageNumber > 0) then
59 call this.createPreviousPageButton()
60 call this.createNextPageButton()
61 endif
62 else
63 set i = this.m_currentPage * thistype.maxPageButtons
64 set exitValue = (this.m_currentPage + 1) * ADialog.maxPageButtons
65 loop
66 exitwhen (i == exitValue or i == this.m_dialogButtons.size())
67 call ADialogButton(this.m_dialogButtons[i]).removeButton()
68 set i = i + 1
69 endloop
70 if (this.m_maxPageNumber > 0) then
71 call this.removePreviousPageButton()
72 call this.removeNextPageButton()
73 endif
74 call DialogClear(this.m_dialog)
75 endif
76 call DialogDisplay(this.m_player, this.m_dialog, displayed)
77 endmethod
78
79 /// @return Returns if the dialog is displayed to player.
80 public method isDisplayed takes nothing returns boolean
81 return this.m_isDisplayed
82 endmethod
83
84 //start members
85
86 public method player takes nothing returns player
87 return this.m_player
88 endmethod
89
90 //members
91
92 public method dialog takes nothing returns dialog
93 return this.m_dialog
94 endmethod
95
96 public method currentPage takes nothing returns integer
97 return this.m_currentPage
98 endmethod
99
100 /// @return Returns the number of the last page (starting from 0).
101 /// For example: If there are three pages this method will return 2.
102 public method maxPageNumber takes nothing returns integer
103 return this.m_maxPageNumber
104 endmethod
105
106 //convenience methos
107
108 public method show takes nothing returns nothing
109 call this.setDisplayed(true)
110 endmethod
111
112 public method hide takes nothing returns nothing
113 call this.setDisplayed(false)
114 endmethod
115
116 //methods
117
118 public method addExtendedDialogButton takes string text, integer shortcut, boolean isQuitButton, boolean doScoreScreen, ADialogButtonAction action returns ADialogButton
119 return ADialogButton.create(this, text, shortcut, isQuitButton, doScoreScreen, action)
120 endmethod
121
122 public method addDialogButton takes string text, integer shortcut, ADialogButtonAction action returns ADialogButton
123 return this.addExtendedDialogButton(text, shortcut, false, false, action)
124 endmethod
125
126 public method addSimpleDialogButton takes string text, integer shortcut returns ADialogButton
127 return this.addExtendedDialogButton(text, shortcut, false, false, 0)
128 endmethod
129
130 public method addDialogButtonInstance takes ADialogButton instance returns integer
131 if (this.m_dialogButtons.size() - this.m_maxPageNumber * ADialog.maxPageButtons == ADialog.maxPageButtons) then
132 set this.m_maxPageNumber = this.m_maxPageNumber + 1
133 endif
134 call this.m_dialogButtons.pushBack(instance)
135 return this.m_dialogButtons.backIndex()
136 endmethod
137
138 public method removeDialogButtonInstance takes ADialogButton instance returns nothing
139 if (this.m_dialogButtons.size() - this.m_maxPageNumber * ADialog.maxPageButtons == 0) then
140 set this.m_maxPageNumber = this.m_maxPageNumber - 1
141 endif
142 call this.m_dialogButtons.remove(instance)
143 endmethod
144
145 public method removeDialogButtonByIndex takes integer index returns nothing
146 if (this.m_dialogButtons.size() - this.m_maxPageNumber * ADialog.maxPageButtons == 0) then
147 set this.m_maxPageNumber = this.m_maxPageNumber - 1
148 endif
149 call this.m_dialogButtons.erase(index)
150 endmethod
151
152 public method dialogButtons takes nothing returns integer
153 return this.m_dialogButtons.size()
154 endmethod
155
156 /// Next and previous page buttons are in vector!
157 public method clear takes nothing returns nothing
158 if (this.m_isDisplayed) then
159 //before page number is changed
160 if (this.m_maxPageNumber > 0) then
161 call this.removePreviousPageButton()
162 call this.removeNextPageButton()
163 endif
164 set this.m_isDisplayed = false
165 endif
166 loop
167 exitwhen (this.m_dialogButtons.empty())
168 call ADialogButton(this.m_dialogButtons.back()).destroy() //removes itself from the vector!
169 endloop
170 set this.m_currentPage = 0
171 call DialogClear(this.m_dialog)
172 endmethod
173
174 /// Friend relation to @struct ADialogButton.
175 public method setDisplayedByButton takes boolean displayed returns nothing
176 set this.m_isDisplayed = displayed
177 endmethod
178
179 private method changeToPreviousPage takes nothing returns nothing
180 call this.setDisplayed(false)
181 if (this.m_currentPage == 0) then
182 set this.m_currentPage = this.m_maxPageNumber
183 else
184 set this.m_currentPage = this.m_currentPage - 1
185 endif
186 call this.setDisplayed(true)
187 endmethod
188
189 private method changeToNextPage takes nothing returns nothing
190 call this.setDisplayed(false)
191 if (this.m_currentPage == this.m_maxPageNumber) then
192 set this.m_currentPage = 0
193 else
194 set this.m_currentPage = this.m_currentPage + 1
195 endif
196 call this.setDisplayed(true)
197 endmethod
198
199 private static method triggerActionPreviousPage takes nothing returns nothing
200 local trigger triggeringTrigger = GetTriggeringTrigger()
201 local thistype this = AHashTable.global().handleInteger(triggeringTrigger, "this")
202 call this.changeToPreviousPage()
203 set triggeringTrigger = null
204 endmethod
205
206 private method createPreviousPageButton takes nothing returns nothing
207 local event triggerEvent
208 local triggeraction triggerAction
209 set this.m_previousPageButton = DialogAddButton(this.m_dialog, thistype.textPreviousPage, thistype.shortcutPreviousPage)
210 set this.m_previousPageTrigger = CreateTrigger()
211 set triggerEvent = TriggerRegisterDialogButtonEvent(this.m_previousPageTrigger, this.m_previousPageButton)
212 set triggerAction = TriggerAddAction(this.m_previousPageTrigger, function thistype.triggerActionPreviousPage)
213 call AHashTable.global().setHandleInteger(this.m_previousPageTrigger, "this", this)
214 set triggerEvent = null
215 set triggerAction = null
216 endmethod
217
218 private static method triggerActionNextPage takes nothing returns nothing
219 local trigger triggeringTrigger = GetTriggeringTrigger()
220 local thistype this = AHashTable.global().handleInteger(triggeringTrigger, "this")
221 call this.changeToNextPage()
222 set triggeringTrigger = null
223 endmethod
224
225 private method removePreviousPageButton takes nothing returns nothing
226 set this.m_previousPageButton = null
227 call AHashTable.global().destroyTrigger(this.m_previousPageTrigger)
228 set this.m_previousPageTrigger = null
229 endmethod
230
231 private method createNextPageButton takes nothing returns nothing
232 local event triggerEvent
233 local triggeraction triggerAction
234 set this.m_nextPageButton = DialogAddButton(this.m_dialog, thistype.textNextPage, thistype.shortcutNextPage)
235 set this.m_nextPageTrigger = CreateTrigger()
236 set triggerEvent = TriggerRegisterDialogButtonEvent(this.m_nextPageTrigger, this.m_nextPageButton)
237 set triggerAction = TriggerAddAction(this.m_nextPageTrigger, function thistype.triggerActionNextPage)
238 call AHashTable.global().setHandleInteger(this.m_nextPageTrigger, "this", this)
239 set triggerEvent = null
240 set triggerAction = null
241 endmethod
242
243 private method removeNextPageButton takes nothing returns nothing
244 set this.m_nextPageButton = null
245 call AHashTable.global().destroyTrigger(this.m_nextPageTrigger)
246 set this.m_nextPageTrigger = null
247 endmethod
248
249 public static method create takes player usedPlayer returns thistype
250 local thistype this = thistype.allocate()
251 //dynamic members
252 set this.m_isDisplayed = false
253 //start members
254 set this.m_player = usedPlayer
255 //members
256 set this.m_dialog = DialogCreate()
257 set this.m_dialogButtons = AIntegerVector.create()
258 set this.m_currentPage = 0
259 set this.m_maxPageNumber = 0
260
261 return this
262 endmethod
263
264 public method onDestroy takes nothing returns nothing
265 call this.clear()
266 set this.m_player = null
267 call DialogDestroy(this.m_dialog)
268 set this.m_dialog = null
269 call this.m_dialogButtons.destroy()
270 endmethod
271
272 public static method init takes integer shortcutPreviousPage, integer shortcutNextPage, string textPreviousPage, string textNextPage returns nothing
273 //static start members
274 set thistype.shortcutPreviousPage = shortcutPreviousPage
275 set thistype.shortcutNextPage = shortcutNextPage
276 set thistype.textPreviousPage = textPreviousPage
277 set thistype.textNextPage = textNextPage
278 endmethod
279 endstruct
280
281 endlibrary